package com.zxl.service.impl;

import com.zxl.service.GoodsService;
import com.zxl.vo.Goods;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @packageName com.zxl.service.impl
 * @ClassName
 * @Author zxl
 * @Description //TODO
 * @date
 **/
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private Configuration configuration;


    @Override
    public void add(Goods goods) throws Exception {
        Template template = configuration.getTemplate("item.ftl");


        Map<String, Object> map = new HashMap<>();
        map.put("gname",goods.getGname());
        map.put("gprice",goods.getGprice());
        map.put("gpng",goods.getGpng().split("\\|"));
        map.put("gdesc",goods.getGdesc());

        ClassLoader classLoader = this.getClass().getClassLoader();
        URL templates = classLoader.getResource("templates");
        String path = templates.getPath();

        template.process(map, new FileWriter(path+ File.separator+goods.getId()+".html"));
    }
}
